//
//  SXPloginViewController.swift
//  weibo
//
//  Created by shixinPeng on 16/2/24.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
import SnapKit

/*
自定义登陆界面
有中心图片,
一个label
两个button
自定义视图的三部曲
1.重写init
2.创建一个私有方法,用来设置视图和添加约束
3.懒加载
*/
//设置代理
protocol SXPloginViewDelegate: NSObjectProtocol{
    //定义连个方法
    func didLogin()
    func didRegister()
}

class SXPloginView: UIView {
    
    //定义一个代理属性
    weak var delegate: SXPloginViewDelegate?

    
    func setInfo(msg:String,image:String,animoution:Bool) {
        
        //赋值
        messageLabel.text = msg;
        iconImageView.image = UIImage(named: image)
        
        if(animoution){//有动画
            homeImageView.hidden = false
            animation()
        }else{
            homeImageView.hidden = true
        
        }
        
    }
    
    //第一步:重新init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //添加控件和设置约束
        setUpLoginUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二步:创建一个私有方法, 添加控件,设置约束
    private func setUpLoginUI() {
        
        //添加控件
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //添加遮罩
        addSubview(maskImageView)
        
        addSubview(homeImageView)
        //设置约束
        setUpLayout()
        
    }
    
    //添加动画
    func animation() {
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        //设置改变值
        anima.toValue = 2 * M_2_PI
        //设置变化频率
        anima.repeatCount = MAXFLOAT
        //设置时间间隔
        anima.duration = 5
        //取消移除动画
        anima.removedOnCompletion = false
        //添加到layer上
        iconImageView.layer.addAnimation(anima, forKey: nil)
       
    }
    
    private func setUpLayout() {
        //设置约束,使用SnapKit
        //通用图片就在界面的中间
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //用户图片在中间
        homeImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //设置文本的约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            //距上面的图片20
            make.top.equalTo(self.iconImageView.snp_bottom).offset(10)
            //中心重合
            make.centerX.equalTo(self.snp_centerX)
            //设置宽度
            make.width.equalTo(216)
        }
        
        //设置registerBtn的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            //距上
            make.top.equalTo(self.messageLabel.snp_bottom).offset(10)
            //左侧与文本看齐
            make.left.equalTo(self.messageLabel.snp_left)
            //宽100 高30
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        //设置login的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            //距上10
            make.top.equalTo(self.messageLabel.snp_bottom).offset(10)
            //与右持平
            make.right.equalTo(self.messageLabel.snp_right)
            //宽100 高30
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        //添加遮罩的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        
    }
    
    //第三步:懒加载

    //通用图片
    private lazy var iconImageView: UIImageView = {
        
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return imageView
    }()
    //用户界面图片
    private lazy var homeImageView: UIImageView = {
        
        let imageview = UIImageView()
        
        imageview.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return imageview
    }()
    
    //添加标签
    private lazy var messageLabel: UILabel = {
       
        let lab = UILabel()
        
        lab.text = "关注某些人,回家你就会有好多惊喜啊"
        //设置居中
        lab.textAlignment = .Center
        //设置字号
        lab.font = UIFont.systemFontOfSize(14)
        //设置换行
        lab.numberOfLines = 0
        
        return lab
    }()

    //添加注册按钮
    private lazy var registerBtn: UIButton = {
        
       let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents:UIControlEvents.TouchUpInside )
        
        return btn
    }()
    
    //添加登陆按钮
    private lazy var loginBtn: UIButton = {
        
       let btn = UIButton(type: UIButtonType.Custom)
        
      btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    private lazy var maskImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imageView
    }()
    
    func register() {
        //调用代理方法
        delegate?.didRegister()
    }

    func login() {
        //print("登陆")  调用代理方法
        delegate?.didLogin()
        
    }
}
